FUNCTION YBC_SWS_CALLBACK_REQUEST.
*"----------------------------------------------------------------------
*"*"本地接口：
*"  IMPORTING
*"     VALUE(IV_REQUESTID) TYPE  YBC_SWS_REQUEST-REQUESTID
*"     VALUE(IV_APPROVERID) TYPE  YBC_SWS_APPROVER-APPROVERID
*"     VALUE(IV_APPLEVEL) TYPE  YBC_SWS_APPROVER-APPLEVEL
*"     VALUE(IV_SENDBACKLEVEL) TYPE  YBC_SWS_APPROVER-APPLEVEL
*"     VALUE(IV_ACTION) TYPE  YBC_SWS_APPROVER-ACTION
*"     VALUE(IV_COMMENTS) TYPE  YBC_SWS_APPROVER-COMMENTS
*"     VALUE(IV_FORWARDAPPRS) TYPE  CHAR200
*"     VALUE(IV_LANGUAGE) TYPE  CHAR10 DEFAULT 'ZH'
*"  EXPORTING
*"     VALUE(ES_MSG) TYPE  BAPIRET2
*"----------------------------------------------------------------------
  DATA:
    LV_FM_CALLBACK    TYPE YBC_SWS_APP_CNFG-FM_CALLBACK_SWS,
    LV_WFCONFID       TYPE YBC_SWS_APP_CNFG-WFCONFID,
    LV_SENDBACKLEVEL  TYPE YBC_SWS_APPLEVEL,
    LV_APPLEVEL_NEXT  TYPE YBC_SWS_APPLEVEL,
    LS_APPROVER       TYPE YBC_SWS_APPROVER,
    LS_REQUEST        TYPE YBC_SWS_REQUEST,
    LV_REQ_STATUS     TYPE YBC_SWS_REQUEST-STATUS,
    LV_REQ_MSG        TYPE YBC_SWS_REQUEST-MESSAGE,
    LS_MSG            TYPE BAPIRET2,
    LV_USERNAME       TYPE TEXT100,
    LS_APPR_USER      TYPE YBC_SWS_USER,
    LV_FINISHED       TYPE CHAR1,   "审批结束
    LV_FINISHED1      TYPE CHAR1,   "第三方系统取到的审批结束
    LV_CS_NOTEND      TYPE CHAR1,   "会签未结束标识
    LT_APPROVER_NEXT  TYPE STANDARD TABLE OF YBCS_SWS_APPROVER, "应用取到的审批者
    LT_APPROVER_NEXT1 TYPE STANDARD TABLE OF YBCS_SWS_APPROVER, "第三方系统取到的审批者
    LT_FVALUE_H       TYPE STANDARD TABLE OF YBCS_SWS_FVALUE,
    LS_WF_01          TYPE YBC_SWS_WF_01,
    LT_WF_01          TYPE STANDARD TABLE OF YBC_SWS_WF_01.

  LS_APPROVER-REQUESTID = IV_REQUESTID.
  LS_APPROVER-APPROVERID = IV_APPROVERID.
  LS_APPROVER-APPLEVEL = IV_APPLEVEL.
  LS_APPROVER-ACTION = IV_ACTION.
  LS_APPROVER-COMMENTS = IV_COMMENTS.
  LV_SENDBACKLEVEL  = IV_SENDBACKLEVEL.

  LV_REQ_STATUS = '1'. "处理中
  MESSAGE S027(YBC_SWS) INTO  LV_REQ_MSG.

  LS_APPROVER-ADATE = SY-DATUM.
  LS_APPROVER-ATIME = SY-UZEIT.
  LS_APPROVER-ACTUALPROCESSED = 'X'.
  LS_APPROVER-PROCESSED = 'X'.

  SELECT SINGLE
    VERSION
    APPLEVEL_DESC1
    APPLEVEL_DESC2
    APPLEVEL_DESC3
    RDATE
    RTIME
    COUNTERSIGN
    INTO ( LS_APPROVER-VERSION, LS_APPROVER-APPLEVEL_DESC1, LS_APPROVER-APPLEVEL_DESC2,
    LS_APPROVER-APPLEVEL_DESC3, LS_APPROVER-RDATE, LS_APPROVER-RTIME, LS_APPROVER-COUNTERSIGN )
    FROM YBC_SWS_APPROVER
   WHERE REQUESTID = LS_APPROVER-REQUESTID
     AND APPROVERID = LS_APPROVER-APPROVERID
     AND APPLEVEL = LS_APPROVER-APPLEVEL
     AND PROCESSED = SPACE.
  IF SY-SUBRC <> 0.
    CLEAR ES_MSG.
    ES_MSG-TYPE = 'E'.
    ES_MSG-ID = 'YBC_SWS'.
    ES_MSG-NUMBER = '001'.
    MESSAGE S002(YBC_SWS) INTO ES_MSG-MESSAGE .
    RETURN.
  ENDIF.

  "获取单据信息
  SELECT SINGLE
    *
    INTO LS_REQUEST
    FROM YBC_SWS_REQUEST
   WHERE REQUESTID = LS_APPROVER-REQUESTID.
  IF SY-SUBRC <> 0 OR LS_REQUEST-BUSINESSID IS INITIAL.
    CLEAR ES_MSG.
    ES_MSG-TYPE = 'E'.
    ES_MSG-ID = 'YBC_SWS'.
    ES_MSG-NUMBER = '001'.
    MESSAGE S002(YBC_SWS) INTO ES_MSG-MESSAGE .
    RETURN.
  ENDIF.

  SELECT SINGLE
    *
    INTO LS_APPR_USER
    FROM YBC_SWS_USER
   WHERE USERID = IV_APPROVERID.
  CONCATENATE LS_APPR_USER-NAME1 LS_APPR_USER-NAME2 LS_APPR_USER-NAME3 INTO LV_USERNAME SEPARATED BY SPACE.

  "等待期间被其它人处理
  IF LS_REQUEST-STATUS <> '0' AND LS_REQUEST-STATUS <> '1'. "LS_REQUEST-MOD <> 'NEW' OR
    CLEAR ES_MSG.
    ES_MSG-TYPE = 'E'.
    ES_MSG-ID = 'YBC_SWS'.
    ES_MSG-NUMBER = '001'.
    MESSAGE S033(YBC_SWS) INTO ES_MSG-MESSAGE .
    RETURN.
  ENDIF.

  CASE IV_ACTION.
    WHEN 'FORWARD'. "处理转发操作
      CALL FUNCTION 'YBC_SWS_FORWARD_REQUEST'
        EXPORTING
          IS_APPROVER     = LS_APPROVER
          IV_FORWARDAPPRS = IV_FORWARDAPPRS
          IV_LANGUAGE     = IV_LANGUAGE
        IMPORTING
          ES_MSG          = ES_MSG.

      RETURN.

    WHEN 'SENDBACK'. "处理回退操作
      CLEAR ES_MSG.
      CALL FUNCTION 'YBC_SWS_SENDBACK_REQUEST'
        EXPORTING
          IS_APPROVER      = LS_APPROVER
          IV_SENDBACKLEVEL = LV_SENDBACKLEVEL
          IV_WFCONFID      = LV_WFCONFID
          IV_LANGUAGE      = IV_LANGUAGE
        IMPORTING
          ES_MSG           = ES_MSG.

      IF ES_MSG-TYPE <> 'E'.
        ES_MSG-TYPE = 'S'.
        ES_MSG-ID = 'YBC_SWS'.
        ES_MSG-NUMBER = '001'.
        MESSAGE S036(YBC_SWS) INTO ES_MSG-MESSAGE.
      ENDIF.

      RETURN.

    WHEN OTHERS. "其他操作继续执行

  ENDCASE.


  "获取回调函数
  CLEAR:
   LV_FM_CALLBACK,
   LV_WFCONFID.

  SELECT SINGLE
    FM_CALLBACK_SWS
    WFCONFID
    INTO ( LV_FM_CALLBACK, LV_WFCONFID )
    FROM YBC_SWS_APP_CNFG
   WHERE APPID = LS_REQUEST-APPID.
  IF LV_FM_CALLBACK IS INITIAL.
    CLEAR ES_MSG.
    ES_MSG-TYPE = 'E'.
    ES_MSG-ID = 'YBC_SWS'.
    ES_MSG-NUMBER = '001'.
    MESSAGE S034(YBC_SWS) INTO ES_MSG-MESSAGE .
    RETURN.
  ENDIF.

**********************************************************************
  "审批的情况下，获取最终审批标识 或 下级审批人
  IF LS_APPROVER-ACTION = 'APPROVE'.
    "处理会签
    IF LS_APPROVER-COUNTERSIGN IS NOT INITIAL.
      SELECT COUNT(*)
        FROM YBC_SWS_APPROVER
       WHERE REQUESTID = LS_APPROVER-REQUESTID
         AND APPROVERID <> LS_APPROVER-APPROVERID
         AND APPLEVEL = LS_APPROVER-APPLEVEL
         AND ACTUALPROCESSED = SPACE.

      IF SY-SUBRC = 0.
        LV_CS_NOTEND = 'X'.
      ENDIF.
    ENDIF.

    "会签未结束，单据还是审批中
    IF LV_CS_NOTEND IS NOT INITIAL.
      "更改SwiftSAP单据状态
      MODIFY YBC_SWS_APPROVER FROM LS_APPROVER .

      UPDATE YBC_SWS_REQUEST SET STATUS = LV_REQ_STATUS
                                MESSAGE = LV_REQ_MSG
          WHERE REQUESTID = LS_APPROVER-REQUESTID.
      COMMIT WORK AND WAIT.

      CLEAR ES_MSG.
      ES_MSG-TYPE = 'S'.
      ES_MSG-ID = 'YBC_SWS'.
      ES_MSG-NUMBER = '001'.
      MESSAGE S036(YBC_SWS) INTO ES_MSG-MESSAGE.
      RETURN.
    ENDIF.

    "检查是否需要下级审批，并获取下级审批人
    "找下级审批标识，并设置审批结束标识
    IF LV_WFCONFID IS NOT INITIAL.
      SELECT *
          INTO CORRESPONDING FIELDS OF TABLE LT_FVALUE_H
          FROM YBC_SWS_REQHEAD
         WHERE REQUESTID = LS_REQUEST-REQUESTID.

      CLEAR LS_MSG.
      CALL FUNCTION 'YBC_SWS_CHECK_FINISHED'
        EXPORTING
          IV_WFCONFID   = LV_WFCONFID
          IV_APPLEVEL   = LS_APPROVER-APPLEVEL
        IMPORTING
          EV_FINISHED   = LV_FINISHED
          ES_MSG        = LS_MSG
        TABLES
          IT_REQ_HEADER = LT_FVALUE_H.

      IF LS_MSG-TYPE = 'E'.
        ES_MSG-ID = 'YBC_SWS'.
        ES_MSG-NUMBER = '001'.
        MESSAGE S001(YBC_SWS) INTO ES_MSG-MESSAGE WITH LS_MSG-MESSAGE .
        RETURN.
      ENDIF.

      IF LV_FINISHED IS INITIAL.
        SELECT *
          INTO TABLE LT_WF_01
          FROM YBC_SWS_WF_01
         WHERE WFCONFID = LV_WFCONFID.

        SORT LT_WF_01 BY APPLEVEL.
        DELETE LT_WF_01 WHERE APPLEVEL <= LS_APPROVER-APPLEVEL.

        CLEAR LS_WF_01.
        READ TABLE LT_WF_01 INTO LS_WF_01 INDEX 1.
        IF LS_WF_01-APPLEVEL IS NOT INITIAL.
          LV_APPLEVEL_NEXT = LS_WF_01-APPLEVEL.
        ELSE.
          LV_FINISHED = 'X'.
        ENDIF.

        "如果需要下级审批且获取不到审批人则报错返回
        IF LV_APPLEVEL_NEXT IS NOT INITIAL.
          CLEAR ES_MSG.
          CALL FUNCTION 'YBC_SWS_GET_APPROVER'
            EXPORTING
              IV_WFCONFID   = LV_WFCONFID
              IV_APPLEVEL   = LV_APPLEVEL_NEXT
            IMPORTING
              ES_MSG        = ES_MSG
            TABLES
              IT_REQ_HEADER = LT_FVALUE_H
              ET_APPROVER   = LT_APPROVER_NEXT.

          IF ES_MSG-TYPE = 'E'.
            ES_MSG-ID = 'YBC_SWS'.
            ES_MSG-NUMBER = '001'.
            MESSAGE S035(YBC_SWS) INTO ES_MSG-MESSAGE WITH ES_MSG-MESSAGE .
            RETURN.
          ENDIF.
        ENDIF.

      ENDIF.
    ENDIF.
  ENDIF.
**********************************************************************


  IF LS_APPROVER-ACTION = 'REJECT'.
    LV_FINISHED = 'X'.
  ENDIF.

  "用户自定义函数调用，更改业务单据状态
  CLEAR: ES_MSG,LV_FINISHED1.
  REFRESH LT_APPROVER_NEXT1[].
  CALL FUNCTION LV_FM_CALLBACK
    EXPORTING
      IV_APPLEVEL      = LS_APPROVER-APPLEVEL
      IV_BUSINESSID    = LS_REQUEST-BUSINESSID
      IV_ACTION        = LS_APPROVER-ACTION
      IV_COMMENTS      = LS_APPROVER-COMMENTS
      IV_SENDBACKLEVEL = LV_SENDBACKLEVEL
      IV_REQUESTID     = IV_REQUESTID
      IV_APPROVERID    = LS_APPROVER-APPROVERID
    IMPORTING
      ES_MSG           = ES_MSG
    TABLES
      ET_APPROVER_NEXT = LT_APPROVER_NEXT1 "第三方系统取到的审批者
    CHANGING
      CV_FINISHED      = LV_FINISHED.       "第三方系统取到的审批结束

  IF ES_MSG-TYPE = 'E'.
    RETURN.
  ENDIF.

  IF LT_APPROVER_NEXT1 IS NOT INITIAL.    "第三方系统取到的审批者
    LT_APPROVER_NEXT = LT_APPROVER_NEXT1.
  ENDIF.

  IF LV_FINISHED1 IS NOT INITIAL.         "第三方系统取到的审批结束
    LV_FINISHED = LV_FINISHED1.
  ENDIF.

  IF LT_APPROVER_NEXT IS INITIAL AND LV_FINISHED IS INITIAL.
    CLEAR ES_MSG.
    ES_MSG-TYPE = 'E'.
    ES_MSG-ID = 'YBC_SWS'.
    ES_MSG-NUMBER = '001'.
    MESSAGE S035(YBC_SWS) INTO ES_MSG-MESSAGE.
    RETURN.
  ENDIF.

  IF LV_FINISHED IS NOT INITIAL.
    CASE LS_APPROVER-ACTION.
      WHEN 'APPROVE'.
        LV_REQ_STATUS = 'S'. "完成
        MESSAGE S029(YBC_SWS) INTO LV_REQ_MSG.
      WHEN 'REJECT'.
        LV_REQ_STATUS = '4'. "已拒绝
        MESSAGE S055(YBC_SWS) INTO LV_REQ_MSG WITH LV_USERNAME.
      WHEN OTHERS.
        LV_REQ_STATUS = 'S'. "完成
        MESSAGE S029(YBC_SWS) INTO LV_REQ_MSG.
    ENDCASE.

  ENDIF.

  "更改SwiftSAP单据状态
  MODIFY YBC_SWS_APPROVER FROM LS_APPROVER .
  "这个审批级别下所有单据打上已处理标识
  UPDATE YBC_SWS_APPROVER SET PROCESSED = 'X'
    WHERE REQUESTID = LS_APPROVER-REQUESTID
      AND APPLEVEL = LS_APPROVER-APPLEVEL.

  UPDATE YBC_SWS_REQUEST SET STATUS = LV_REQ_STATUS
                            MESSAGE = LV_REQ_MSG
      WHERE REQUESTID = LS_APPROVER-REQUESTID.

  COMMIT WORK AND WAIT.

  IF SY-SUBRC = 0.
    IF LV_FINISHED IS INITIAL. "发送到下一级别审批人
      CALL FUNCTION 'YBC_SWS_SEND_NEXT'
        EXPORTING
          IV_REQUESTID = LS_APPROVER-REQUESTID
        IMPORTING
          ES_MSG       = ES_MSG
        TABLES
          IT_APPROVER  = LT_APPROVER_NEXT.

      IF ES_MSG-TYPE = 'E'.
        RETURN.
      ENDIF.
    ENDIF.

    ES_MSG-TYPE = 'S'.
    ES_MSG-ID = 'YBC_SWS'.
    ES_MSG-NUMBER = '001'.
    MESSAGE S036(YBC_SWS) INTO ES_MSG-MESSAGE.

  ELSE.
    ROLLBACK WORK.
    ES_MSG-TYPE = 'E'.
    ES_MSG-ID = 'YBC_SWS'.
    ES_MSG-NUMBER = '001'.
    MESSAGE S037(YBC_SWS) INTO ES_MSG-MESSAGE.
  ENDIF.
ENDFUNCTION.
